// http://www.znz888.com/stock/realstock.php?code=sh000001s

const Koa = require("koa")
const koaBody = require("koa-body")

const Router = require("koa-router")
const Stock = require("./modules/Stock")
const GuoXin = require("./modules/GuoXin")
const QuantitativeTrading = require("./modules/QuantitativeTrading")

const app = new Koa()
const router = new Router()

app.use(koaBody({ multipart: true }))

// 设置跨域中间件
app.use(async (ctx, next) => {
    ctx.set('Access-Control-Allow-Origin', '*'); // 允许所有域名跨域访问
    ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); // 允许的请求方法
    ctx.set('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // 允许的请求头字段
    if (ctx.method === 'OPTIONS') {
        ctx.status = 204
    }
    else {
        let startTime = +new Date()

        ctx.body = { code: 200, data: null, msg: "", time: 0 }
        try {
            await next()
        }
        catch (e) {
            ctx.body.code = 500
            ctx.body.msg = JSON.stringify(e)
            console.log(e)
        }

        ctx.body.time = +new Date() - startTime
    }
});

require("./router")({ router })

new GuoXin()
new QuantitativeTrading()

// 启动路由
app.use(router.routes()).use(router.allowedMethods())

app.listen(8081)
